<?php

class module_base
{
    protected $_succ = 'succ';
    protected $_fail = 'fail';

    public function __construct()
    {
        $this->_data_base = new data_base();

    }

	/**
	* 看是否有data对象，如果没有则创建
	* @return string
	*/
	protected function new_data_class($class_name)
	{
		if( isset( $GLOBALS['instance'][$class_name] )
				&& $GLOBALS['instance'][$class_name] instanceof $class_name )
		{
			return $GLOBALS['instance'][$class_name];
		}
		else
		{
			$new_data_class = new $class_name;
			$GLOBALS['instance'][$class_name] = $new_data_class;
		}
		return $new_data_class;
	}

	/**
	* 组装sql条件语句
	* @return string
	*/
	public function build_condition($array)
	{
		$condition = '';
		if($array && is_array($array))
		{
			foreach($array as $k => $v)
			{
				if($v)
				{
					$condition .= " AND $k = '$v' ";
				}
			}
		}

		return $condition;
	}
    /**
     * 组装and条件sql条件语句
     * @return string
     */
    public function build_condition_by_and( $array )
    {
        $condition = TRUE;
        foreach ( $array as $key => $val )
        {
            if ( in_array( $key, array( 'mod', 'act', 'p' ) ) || $val == '' ) continue;
            if ( $condition != TRUE )
            {
                $condition = " AND {$key} = '{$val}' ";
            }
            else
            {
                $condition = " {$key} = '{$val}' ";
            }

        }
        return $condition;
    }
    /**
     *
     * 页面跳转
     * @param
     */
    public function go( $mod, $act )
    {
        $controller = "controller_{$mod}";
        $class = new $controller();
        $class->$act($_GET);
    }
    /**
     *
     * 获取当前用户合作伙伴ID
     */
    static function get_partnerid()
    {
        return isset($_COOKIE['swsadmin_partnerid'])?$_COOKIE['swsadmin_partnerid']:'';
    }
    /**
     *
     * 获取当前用户合作伙伴ID
     */
    static function get_user_id()
    {
        return $_COOKIE['swsadmin_uid'];
    }

    /**
     *
     * 获取当前用户名称
     */
    static function get_user_name()
    {
        return $_COOKIE['swsadmin_uname'];
    }

    /**
     *
     * 获取当前用户登录信息
     */
    static function get_login_user_info()
    {
        $data_admin_users    = new data_admin_users();
        $user_info = $data_admin_users->fetch_by_id( $_COOKIE['swsadmin_uid'] );
        $user_info['user_id'] = $user_info['id'];
        return $user_info;
    }

    /**
     * 判断当前用户是否为管理员
     * @return <type>
     */
    static function is_admin()
    {
        if(self::get_partnerid()){
            return false;
        }  else {
            return true;
        }
    }

    /**
     * 将要输出的数据格式成需要的形式返回
     * @param boolean $is_succ
     * @param mixed   $info
     * @return array
     */
    protected function _formatreturndata($is_succ, $info = null)
    {
        $res = array();
        if($is_succ) {
            $res['result'] = $this->_succ;
            $res['info'] = $info;
        } else {
            $res['result'] = $this->_fail;
            $res['reason'] = $info;
        }
        return $res;
    }


     protected function _formatreturnlist($is_succ, $info = null)
    {
        $res = array();
        if($is_succ) {
            $res['result'] = $this->_succ;
            $res['num'] = count($info);
            $res['list'] = $info;
        } else {
            $res['result'] = $this->_fail;
            $res['reason'] = $info;
        }
        return $res;
    }

    /**
     * 把二维数组转化为指定下标值为下标的二维数组
     *
     * @param array $data
     * @param string $key
     * @return array
     * @example
     * $data = array(array('id'=>'35', 'name'=> 'test'),
     *               array('id'=>'36', 'name'=> 'test'))
     * $re = $this->array_convert($data, 'id');
     * 结果$re为
     * array('35' => array('id'=>35, 'name'=> 'test'),
     *       '36' => array('id'=>36, 'name'=> 'test'))
     */
    public function array_convert(array $data, $key)
    {
        $tmp = array();
        foreach ($data as $r)
        {
            $tmp[$r[$key]] = $r;
        }
        return $tmp;
    }
}
